﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProgLab.CalendarSyncUtil;

using MSOffice = Microsoft.Office.Interop;
using System.Collections;

namespace UT
{
    public class OutlookCalendarBook : CalendarBook<MSOffice.Outlook.Folder>
    {
        public override string Name
        {
            get
            {
                return this.calendar == null ? "" : this.calendar.Name;
            }
        }

        public override string Summary
        {
            get
            {
                return this.calendar == null ? "" : this.calendar.Name;
            }
        }

        public override DateTime CreateDate
        {
            get
            {
                return DateTime.MinValue;
            }
        }

        public OutlookCalendarBook(MSOffice.Outlook.Folder data)
            : base(data)
        {
        }

        public override bool AddAppointment(ICalendarItem src)
        {
            throw new NotImplementedException();
        }
    }



    /// <summary>
    /// Google 行事曆
    /// </summary>
    public class OutlookCalendarSource : CalendarSource
    {
        protected Microsoft.Office.Interop.Outlook.Application olapp = new Microsoft.Office.Interop.Outlook.Application();

        public override void Query(DateTime start, DateTime end)
        {
            MSOffice.Outlook.Folder calFolder =
                olapp.Session.GetDefaultFolder(
                MSOffice.Outlook.OlDefaultFolders.olFolderCalendar)
                    as MSOffice.Outlook.Folder;

            Query(calFolder, start, end);
        }

        public override bool UpdateCalendar(ICalendarItem item)
        {
            throw new NotImplementedException();
        }

        public override bool UpdateCalendar(ICalendarItem myItem, ICalendarItem srcItem)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 查詢指定日曆的資料
        /// </summary>
        /// <param name="calendar"></param>
        public void Query(OutlookCalendarBook calendar, DateTime start, DateTime end)
        {
            MSOffice.Outlook.Folder olFolder = calendar.MyCalendar;

            Query(olFolder, start, end);

        }

        protected void Query(MSOffice.Outlook.Folder olFolder, DateTime start, DateTime end)
        {
            this.CalendarItems.Clear();
            string filter = "[Start] >= '"
               + start.ToString("g")
               + "' AND [End] <= '"
               + end.ToString("g") + "'";

            MSOffice.Outlook.Items calItems = olFolder.Items;

            if (calItems.Count > 0)
            {

                calItems.IncludeRecurrences = true;
                calItems.Sort("[Start]", Type.Missing);
                MSOffice.Outlook.Items restrictItems = calItems.Restrict(filter);

                foreach (MSOffice.Outlook.AppointmentItem myapp in restrictItems)
                {
                    this.CalendarItems.Add(new OutlookCalendarItem(myapp));
                }
            }

        }

        ///
        /// 不可以跨組件界限使用，因為它包含的型別具有內嵌 Interop 型別的泛型型別參數
        /// Outlook 好像只支援一個行事曆
        ///
        //public List<OutlookCalendarBook> QueryAllCalendar()
        public ArrayList QueryAllCalendar()
        {
            MSOffice.Outlook.Folder olfolder = 
                (MSOffice.Outlook.Folder)olapp.Session.GetDefaultFolder(MSOffice.Outlook.OlDefaultFolders.olFolderCalendar);   

            //List<OutlookCalendarBook> result = new List<OutlookCalendarBook>();
            ArrayList result = new ArrayList();

            result.Add(new OutlookCalendarBook(olfolder));            

            return result;
        }

        public OutlookCalendarBook QueryCalendar(string calendarName)
        {
            //List<OutlookCalendarBook> allCalendar = QueryAllCalendar();
            ArrayList allCalendar = QueryAllCalendar();
            foreach (OutlookCalendarBook entry in allCalendar)
            {
                if (entry.Name == calendarName)
                    return entry;
            }

            return null;
        }
    }


    public class OutlookCalendarItem : CalendarItem
    {
        protected MSOffice.Outlook.AppointmentItem data = null;

        public OutlookCalendarItem(MSOffice.Outlook.AppointmentItem data)
        {
            this.data = data;

            this.subject = this.data.Subject;
            this.content = this.data.Body == null ? "" : this.data.Body;
            this.Location = this.data.Location == null ? "" : this.data.Location;              

            this.Start = this.data.Start;
            this.end = this.data.End;

            this.isAllDay = this.data.AllDayEvent; 
        }

        public void Update()
        {
            if (this.data != null)
                this.data.Save();
        }
    }
}
